#include <gnuplotter.h>

int getmoremem( datapt_t ** ptr, int memtot, options_t * options ) {

	if ( memtot == 0 ) {
		memtot = 500;
		*ptr = NULL;
	}
	else
		memtot *= 2;

	*ptr = realloc(*ptr, memtot*sizeof(datapt_t));
	if ( ptr == NULL ) {
		fprintf(stderr,"error: malloc failed. requested %d bytes.\n", memtot*sizeof(datapt_t));
		cleanup(options);
	}

	return memtot;
}

//read in the data
void read(options_t * options) {
	char buffer[BUFFSIZE];
	int memtot, memused, test;
	memtot = 0; memused = 0;

	memtot = getmoremem(&(options->readarray), memtot, options);

	//read data
	while( fgets(buffer,BUFFSIZE,stdin) ) {
		if ( memused >= memtot )  //we're going to run out of mem
			memtot = getmoremem(&(options->readarray),memtot, options);
		//read in the data
		test = sscanf(buffer,"%lf %lf %lf",
			&(options->readarray[memused].x[0]), 
			&(options->readarray[memused].x[1]), 
			&(options->readarray[memused].x[2])
		);
		if ( test != 3 ) {  //make sure we read three numbers
			fprintf(stderr,"error: failed to read three coordinates on line %d.\n", memtot);
			cleanup(options);
		}
		memused++;
	}

	return;
}

